//
//  FWCHomeViewModel.swift
//  微博项目001
//
//  Created by 樊武超 on 16/4/12.
//  Copyright © 2016年 樊武超. All rights reserved.
//

import UIKit
private let userInfoKey = "com.itheima.userifonKey"
class FWCHomeViewModel: NSObject {
    
    
    var userMessage: FWCUserMessageModel?
 
    //加载头像
    var iconImageUrl:NSURL?{
    let url=NSURL(string: userMessage?.avatar_large ?? "")
        
        return url
    
    }
    
    private override init() {
        super.init()
        userMessage=self.loadUserInfor()
    
    }
    
    static let shareHomeViewModel: FWCHomeViewModel=FWCHomeViewModel()
    
    var isLogin: Bool{
    
    return userMessage?.access_token != nil
    }
   
    
    func loadViewController(code: String,finshed: (issuccess: Bool) -> ()){
    
    FWCNetworkTools.shareNetworkTool.loadViewController(code) { (result, error) -> () in
        if error != nil{
            print(error)
            print("数据加载有问题1")
            finshed(issuccess: false)
            return
        }
        
        self.loadUserInfor(result!, finshed: finshed)
        
        
        }
    }
    func loadUserInfor(result: AnyObject,finshed: (issuccess: Bool) -> ()){
        guard let dict = result as? [String: AnyObject] else{
            print("加载不成功2")
            finshed(issuccess: false)
            return
        }
    
        let token=dict["access_token"]!
        let uid=dict["uid"]!
        
        FWCNetworkTools.shareNetworkTool.loadUserInfor(token, uid: uid) { (result, error) -> () in
            if error != nil{
            
                print(error)
                print("数据加载不正确3")
                finshed(issuccess: false)
                return
            }
            
            guard var userInfoDict = result as? [String: AnyObject] else{
            finshed(issuccess: false)
                return
            
            }
            for (key,value) in dict {
            userInfoDict[key]=value
            
            }
            self.saveUserMessage(userInfoDict)
            
            finshed(issuccess: true)
        }
    
    }
    //将获取的数据存储到沙盒
    func saveUserMessage(userInfoDict:[String : AnyObject]){
    
    
        let userDefaults=NSUserDefaults.standardUserDefaults()
        
        let userMessage=FWCUserMessageModel(dict: userInfoDict)
        
        self.userMessage=userMessage
        
        //将获取的模型转换为字典类型
        
        let keys=["access_token","avatar_large","name","expires_in","uid","expires_date"]
        let messageDict=userMessage.dictionaryWithValuesForKeys(keys)
        
        userDefaults.setObject(messageDict, forKey: userInfoKey)
        userDefaults.synchronize()
        print(NSHomeDirectory())
       }
    //获取沙盒中的数据
    func loadUserInfor() -> FWCUserMessageModel?{
        guard let dict=NSUserDefaults.standardUserDefaults().objectForKey(userInfoKey) as? [String: AnyObject] else{
        
        
        return nil
        }
    
        let userMessageModel=FWCUserMessageModel(dict: dict)
        
        return userMessageModel
    
    }
}
